package com.dycong.common.leetcode.yi;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: yilin
 * Date: 2019-08-19
 * Time: 19:12
 */
public class RevertX {

    public int reverse(int x) {
        int ans = 0;
        while (x != 0) {
            int pos = x % 10;
            if (ans > Integer.MAX_VALUE / 10 || (Integer.MAX_VALUE == ans && pos > 7)) {
                return 0;
            } else if (ans < Integer.MIN_VALUE / 10 || (ans == Integer.MIN_VALUE && pos < -8)) {
                return 0;
            }
            ans = ans * 10 + pos;
            x /= 10;
        }

        return ans;
    }
}
